package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.lang.Validator;
import com.dog.basics.bean.dto.admin.mtkj.WheelchairTreatyDto;
import com.dog.basics.bean.dto.netty.NettyCommonDto;
import com.dog.basics.bean.dto.netty.NettyWheelchairDto;
import com.dog.basics.bean.model.mtkj.MtDeviceLock;
import com.dog.basics.bean.model.mtkj.MtDeviceLockPass;
import com.dog.basics.common.util.SpringContextUtils;
import com.dog.basics.service.mtkj.IMtDeviceLockPassService;
import com.dog.basics.service.mtkj.IMtDeviceLockService;
import com.dog.basics.service.mtkj.WheelchairTreatyService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @description: 轮椅定制协议业务实现
 * @author: He
 * @date: Created in 2020/10/14 12:20
 * @version: 1.0
 * @modified By:
 */
@Service
@AllArgsConstructor
public class WheelchairTreatyServiceImpl implements WheelchairTreatyService {

    private final IMtDeviceLockService deviceLockService;

    private final IMtDeviceLockPassService deviceLockPassService;

    /**
     * @功能描述: 轮椅定制协议远程控制指令
     * @author He
     * @date 2020/10/14
     */
    @Override
    public Boolean sendWheelchairTreatyInstructions(WheelchairTreatyDto dto) {
        // 查询设备是否存在
        MtDeviceLock deviceLock = deviceLockService.getById(dto.getDevId());
        Validator.validateNotNull(deviceLock, "设备信息不存在");
        // 判断是什么命令指令
        NettyWheelchairDto build = null;
        switch (dto.getMsgType()) {
            case 14:
                // 14-开锁测试
                // 查询-锁通道
                MtDeviceLockPass deviceLockPass = deviceLockPassService.getById(dto.getLockportId());
                Validator.validateNotNull(deviceLockPass, "锁通道不存在");
                // 判断当前锁通道是否在当前锁上
                Validator.validateEqual(deviceLock.getId(), deviceLockPass.getLockId(), "当前选择的设备,与锁通道不符!");
                build = NettyWheelchairDto.builder().msgType(dto.getMsgType()).devId(dto.getDevId()).lockport(Integer.parseInt(deviceLockPass.getPassNum())).build();
                break;
            case 15:
                // 15-查询轮椅状态（锁状态）
                build = NettyWheelchairDto.builder().msgType(dto.getMsgType()).devId(dto.getDevId()).build();
                break;
            default:
                Validator.validateTrue(Boolean.FALSE, "未知的指令操作");
                break;
        }
        SpringContextUtils.getBean(RabbitTemplate.class).convertAndSend("EX-WHEELCHAIR-MQ", "SERVICE-COMMON-WHEELCHAIR", build);
        return Boolean.TRUE;
    }

}